package com.ocm.tourism.webclient.service;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.ocm.tourism.core.util.TourismUtil;
import com.ocm.tourism.webclient.model.WebClientModel;

public class BaoTayNinhServiceImpl implements WebClientService {

   private String location;

   private int numberRecords;;

   @Override
   public List<WebClientModel> getWebClientModels() {
      try{
         URL url = new URL(location);
         String content = TourismUtil.convert(url.openStream());
         List<WebClientModel> webClientModels = parse(content);
         return webClientModels;
      }catch (Exception e) {
         System.out.println(e);
         //throw new TourismRuntimeException(e.getMessage());
      }
      return null;
   }

   private List<WebClientModel> parse(String content){
      String staticDelimStr = "class=\"text-arblo-12\"";
      List<WebClientModel> webClientModels = new ArrayList<WebClientModel>();
      int indexof = content.indexOf(staticDelimStr);
      String firstSubString = content.substring(0, indexof);
      String nextSubString = content.substring(indexof + staticDelimStr.length() + 1);
      int nextIndexOf = 0;
      while (nextIndexOf >=0 && webClientModels.size() <= numberRecords){
         nextIndexOf = nextSubString.indexOf(staticDelimStr);
         String webClientContent = nextIndexOf >= 0 ? nextSubString.substring(0, nextIndexOf) : nextSubString;
         WebClientModel webClientModel = getWebClientModel(webClientContent, firstSubString);
         if (webClientContent != null)
            webClientModels.add(webClientModel);
         firstSubString = webClientContent;
         if (nextIndexOf >= 0)
            nextSubString = nextSubString.substring(nextIndexOf + staticDelimStr.length() + 1);
         
      }
      return webClientModels;
   }

   private WebClientModel getWebClientModel(String content, String imageContent){
      String nextSubString = content;
      String delimStr = "src=";
      int indexof = imageContent.lastIndexOf(delimStr);
      String firstSubString = imageContent.substring(indexof+delimStr.length()+1);
      delimStr = "\"";
      indexof = firstSubString.indexOf(delimStr);
      String imageSrc = firstSubString.substring(0, indexof);

      delimStr = "&quot;";
      indexof = firstSubString.lastIndexOf(delimStr);
      if (indexof < 0)
         return null;
      firstSubString = firstSubString.substring(0, indexof);
      indexof = firstSubString.lastIndexOf(delimStr);
      String href =  firstSubString.substring(indexof + delimStr.length());

      delimStr = "</a>";
      indexof = nextSubString.indexOf(delimStr);
      firstSubString = nextSubString.substring(0, indexof);
      delimStr = "\"";
      indexof = firstSubString.lastIndexOf(delimStr);
      String title = firstSubString.substring(indexof + delimStr.length() + 1);

      delimStr = "class=\"text-ar-12\"";
      indexof = nextSubString.indexOf(delimStr);
      nextSubString = nextSubString.substring(indexof);
      delimStr = "</a>";
      indexof = nextSubString.indexOf(delimStr);
      firstSubString = nextSubString.substring(delimStr.length() + 1, indexof);
      delimStr = "\"";
      indexof = firstSubString.lastIndexOf(delimStr);
      String modelContent = firstSubString.substring(indexof + delimStr.length() + 1);

      WebClientModel webClientModel = new WebClientModel();
      webClientModel.setContent(modelContent);
      webClientModel.setHref(href);
      webClientModel.setImage(imageSrc);
      webClientModel.setTitle(title);
      webClientModel.setBaseUrl(location);
      return webClientModel;
   }

   public String getLocation() {
      return location;
   }

   public void setLocation(String location) {
      this.location = location;
   }

   public int getNumberRecords() {
      return numberRecords;
   }

   public void setNumberRecords(int numberRecords) {
      this.numberRecords = numberRecords;
   }
   
   public static void main(String[] args) {
      //BaoTayNinhServiceImpl baoTayNinhService = new BaoTayNinhServiceImpl();
      //baoTayNinhService.setLocation("http://translate.google.com.vn?hl=en&tab=wT#auto/vi/i%20love%20you%20so%20much");
      //baoTayNinhService.setNumberRecords(5);
      //baoTayNinhService.getWebClientModels();
      /*HttpClient client = new DefaultHttpClient();
      HttpGet get = new HttpGet("http://translate.google.com.vn/?hl=en&tab=wT#auto/vi/i%20love%20you%20so%20much");
      try {
         HttpResponse response = client.execute(get);
         StatusLine line = response.getStatusLine();
         if (line.getStatusCode() > 201) {
            throw new Exception("Fail Retreive files");
         }
         HttpEntity entity = response.getEntity();
         if (entity != null) {
            InputStream is = entity.getContent();
            String str = TourismUtil.convert(is);
        }
      }catch (Exception e) {
         get.abort();
         client.getConnectionManager().shutdown();
         throw new TourismRuntimeException(e.getMessage());
      }*/
   }
}
